package {{.table}}

import (
	"github.com/gin-gonic/gin"
)

// 批量创建 {{.schema.Opts.TableComment}}
func Create(c *gin.Context) {
	var form {{.schema.Opts.UpperCamelCaseTable}}
	c.Bind(&form)
	target, err := New{{.schema.Opts.UpperCamelCaseTable}}Service().Create{{.schema.Opts.UpperTable}}(&form)
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
	}
	c.JSON(200, target)
}

// 根据 ids 删除 {{.schema.Opts.TableComment}}
func DeleteById(c *gin.Context) {
	id := {{.schema.Opts.UpperCamelCaseTable}}Id(c.Param("id"))
	err := New{{.schema.Opts.UpperCamelCaseTable}}Service().Delete{{.schema.Opts.UpperTable}}ById(&id)
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
	}
	c.JSON(200, gin.H{"success": true})
}

// 根据 id 修改 {{.schema.Opts.TableComment}}
func UpdateById(c *gin.Context) {
	id := {{.schema.Opts.UpperCamelCaseTable}}Id(c.Param("id"))
	var form {{.schema.Opts.UpperCamelCaseTable}}
	c.Bind(&form)
	err := New{{.schema.Opts.UpperCamelCaseTable}}Service().Update{{.schema.Opts.UpperTable}}ById(&id, &form)
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
	}
	c.JSON(200, gin.H{"success": true})
}

// 根据搜索条件和分页查找 {{.schema.Opts.TableComment}} 列表
func FindAll(c *gin.Context) {
	list, err := New{{.schema.Opts.UpperCamelCaseTable}}Service().FindAll{{.schema.Opts.UpperTable}}()
	if err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
	}
	c.JSON(200, list)
}
